package com.eway.viewanimator.weidth;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;

import com.eway.viewanimator.R;

/**
 * 作者：Administrator
 * 时间：on 2019/1/25 15:22
 * 功能：
 */
public class LoadingVauleWidth extends ImageView {

	private static String TAG = LoadingVauleWidth.class.getSimpleName();
	private int mTop;
	private int mCount = 2;
	private int mNum = 0;

	public LoadingVauleWidth(Context context) {
		super(context, null);
	}

	public LoadingVauleWidth(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		Log.d(TAG, "layout的位置是" + "left的值" + left + "top的值是" + top + "right的值是" + right + "bottom的值是" + bottom);
		mTop = top;
	}


	private void init() {
		ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100, 0);
		valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
		valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
		valueAnimator.setDuration(2000);
		valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());


		valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				int changeValue = (int) animation.getAnimatedValue();
				setTop(mTop - changeValue);
			}
		});


		valueAnimator.addListener(new Animator.AnimatorListener() {

			@Override
			public void onAnimationStart(Animator animation) {
				setImageDrawable(getResources().getDrawable(R.drawable.list_icon_gif_playing1));
			}

			@Override
			public void onAnimationEnd(Animator animation) {

			}

			@Override
			public void onAnimationCancel(Animator animation) {

			}

			@Override
			public void onAnimationRepeat(Animator animation) {
				mNum++;
				switch (mNum % mCount) {
					case 1:
						setImageDrawable(getResources().getDrawable(R.drawable.list_icon_gif_playing2));
						break;
					case 0:
						setImageDrawable(getResources().getDrawable(R.drawable.list_icon_gif_playing1));
						break;
				}
			}
		});


		valueAnimator.start();
	}


}
